package com.teamwork.ZuccEcho.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.teamwork.ZuccEcho.Entity.Answer;
import com.teamwork.ZuccEcho.Entity.Answersheet;
import com.teamwork.ZuccEcho.Entity.Qnquestion;
import com.teamwork.ZuccEcho.Entity.Questionnaire;
import com.teamwork.ZuccEcho.Repository.AnswerRepository;
import com.teamwork.ZuccEcho.Repository.QnquestionRepository;
import com.teamwork.ZuccEcho.Repository.StudentFeedBackApiRepository;
import com.teamwork.ZuccEcho.Result.ExceptionMsg;
import com.teamwork.ZuccEcho.Result.Response;
import com.teamwork.ZuccEcho.Result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("api/answersheet")
public class StudentFeedBackController {
    protected Response result(ExceptionMsg msg){
        return new Response(msg);
    }
    protected Response result(){
        return new Response();
    }

    @Autowired
    private StudentFeedBackApiRepository studentFeedBackApiRepository; //导入填写反馈的repository
    @Autowired
    private QnquestionRepository qnquestionRepository; //导入反馈题目的repository
    @Autowired
    private AnswerRepository answerRepository; //导入答卷回答的repository
    @Autowired
    RestTemplateBuilder restTemplateBuilder; //导入restTemplateBuilder

    //TODO 反馈填写，学生填写问卷，传入问卷id、学生id，返回学生的答卷
    @RequestMapping(value = "/write/{qn_id}/v1/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData writeFeedback(@PathVariable("qn_id") long qn_id,@RequestBody Map<String,Object> body){
        String stu_id=String.valueOf(body.get("stu_id"));//获取学生id
        //创建一条答卷记录
        studentFeedBackApiRepository.addAnswersheet(qn_id,stu_id,null);
        //查找刚创建的答卷的id
        long as_id=studentFeedBackApiRepository.findAnswersheetId();
        //下面获取学生对题目的回答，并添加到数据库中
        List<JSONObject> list=JSON.parseObject(JSON.toJSONString(body.get("answer_list")),List.class);//转为json对象列表
        for(int i=0;i<list.size();i++){ //遍历json对象列表
            int a_qtypes= Integer.parseInt(list.get(i).getString("a_qtypes"));//获取题目种类
            String a_content=list.get(i).getString("a_content");//获取回答
            studentFeedBackApiRepository.addAnswer(as_id,a_qtypes,a_content);//添加一条Answer记录
        }
        return new ResponseData(ExceptionMsg.SUCCESS,"创建答卷成功！");
    }

    //TODO 查看指定的反馈，传入学生学号和想查看的历史反馈的id，返回题目和该学生的回答
    @RequestMapping(value = "/history/{qn_id}/v1/", method = RequestMethod.GET)
    public ResponseData checkHistoricalFeedback(@PathVariable("qn_id") long qn_id,@RequestBody Map<String,Object> body){
        String stu_id=String.valueOf(body.get("stu_id"));//获取学生id
        //根据qn_id查询反馈中的题目
        List<Qnquestion> questions=qnquestionRepository.findQuestions(qn_id);
        //根据qn_id和stu_id查找该学生对指定问卷的答卷的id
        long as_id=studentFeedBackApiRepository.findAnswersheet(qn_id,stu_id).getAs_id();
        //根据as_id找到这份问卷的所有题目的回答
        List<Answer> answers=answerRepository.findAnswers(as_id);
        //将题目和回答加入到Map中
        Map<String,Object> map=new HashMap<>();
        int size=questions.size();//题目的数量（也就是answer的数量）
        for(int i=0;i<size;i++){//遍历
            map.put(questions.get(i).getQnq_name(),answers.get(i).getA_content());
        }
        return new ResponseData(ExceptionMsg.SUCCESS,map);
    }

    //TODO 查看反馈列表，传入学生学号，返回此学生填写的所有问卷（反馈）【包括题目和回答】
    @RequestMapping(value = "/history/all/v1/", method = RequestMethod.GET)
    public ResponseData checkFeedbackList(@RequestBody Map<String,Object> body){
        String stu_id=String.valueOf(body.get("stu_id"));//获取学生id
        //根据stu_id查找该学生填过的所有问卷的答卷
        List<Answersheet> answersheets=studentFeedBackApiRepository.findAnswersheets(stu_id);//先找答卷
        //再找答卷id和问卷id
        List<Long> asids=new ArrayList<>();//存放答卷id的列表
        List<Long> qnids=new ArrayList<>();//存放问卷id的列表
        for(int i=0;i<answersheets.size();i++){//遍历每份答卷
            asids.add(answersheets.get(i).getAs_id());//找答卷id，放到列表中
            qnids.add(answersheets.get(i).getQuestionnaire().getQn_id());//找问卷id，放到列表中
        }
        Map<String,Object> map=new HashMap<>();//将问卷id列表和答卷id列表加入到Map中
        map.put("as_id",asids);
        map.put("qn_id",qnids);
        return new ResponseData(ExceptionMsg.SUCCESS,map);
    }
}
